"""
@name : tnn-hw
@author : tnn
@projectname: 天龙八部
"""
#1、多进程实现网段存货检测
import IPy
import time
from pythonping import ping
from multiprocessing import Pool


def ip_text(ip):
    res = ping(ip, timeout=1)
    time.sleep(2)
    if res.success():
        print(str(ip)+':' + "up")
        p_lst_used.append(str(ip))
    else:
        print(str(ip)+":"+"down")
        p_lst_no.append(str(ip))


ip_net = '192.168.73.0/24'
ips = IPy.IP(ip_net)
p_lst_used = []
p_lst_no = []

if __name__ == "__main__":
    pool = Pool(processes=4, maxtasksperchild=4)
    for ip in ips:
        pool.apply_async(func=ip_text, args=(str(ip),))
    pool.close()
    pool.join()

    print("end...")
    print(p_lst_used)
    print(p_lst_no)

#2、任意网站   三创官网爬取图片
#       图片a.jpg
#https://www.sanchaungedu.cn/a.jpg
#requests  请求图片地址，将获取到的图片保存到本地（open）
# import requests
# import re
# url = "https://www.sanchuangedu.cn/"
# response = requests.get(url=url)
# print(response.text)
# imgs = re.findall(r"(?<=<img src=).*(?= width)", response.text)
# print(imgs)
# for i in imgs:
#     # 请求图的二进制数据
#     img = requests.get(url + i).content
#     with open(f"./{i}", "wb+") as fp:
#         fp.write(img)
